﻿<!DOCTYPE>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=EDGE;IE=11;IE=8,chrome=1" />
    <title>排产结果审核确认</title>
    <link rel="stylesheet" type="text/css" href="/InSiteWebApplication/Content/AnyGantt/css/anychart-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="/InSiteWebApplication/Content/AnyGantt/fonts/css/anychart-font.min.css" />
    <link href="/InSiteWebApplication/Content/Easyui/themes/metro/easyui.css" rel="stylesheet" />
    <link href="/InSiteWebApplication/Content/Easyui/custom-easyui.css" rel="stylesheet" />
    <link href="/InSiteWebApplication/Content/Easyui/themes/icon.css" rel="stylesheet" />
    <style type="text/css">
       .datagrid-cell{padding:0}
       .ganttDiv{
            position: absolute;
            margin-top: 8px;
            height: 15px
            }
            .treegrid-tr-tree .tree-checkbox{display:none}
    </style>
</head>
<body>
    <div class="panelSection">
        <div class="easyui-panel " title="查询区域" style="padding: 3px;width: 100%" data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
            <form id="searchForm" method="post" style="margin-bottom: 0;">
            <table>
                <tr class="divLabel">
                    <td>订单号:</td>
                    <td>发动机号:</td>
                    <td>任务单号:</td>
                    <td>计划开始时间</td>
                    <td></td>
                    <td>计划结束时间</td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td><input id="txtMfgorderName" class="easyui-textbox" name="MfgorderName" type="text"style="width: 120px; height: 22px" /></td>
                    <td><input id="txtMachine" class="easyui-textbox" name="Machine" type="text" style="width: 120px;height: 22px" /></td>
                    <td><input id="txtContainerNo" class="easyui-textbox" name="ContainerNo" type="text"style="width: 120px; height: 22px" /></td>
                    <td><input id="txtPlanStartTime" type="text" name="PlanStartTime" class="easyui-datebox"style="width: 110px; height: 22px" /></td>
                    <td>-</td>
                    <td><input id="txtPlanEndTime" type="text" name="PlanEndTime" class="easyui-datebox"style="width: 110px; height: 22px" /></td>
                    <td><a href="javascript:void(0)" class="easyui-linkbutton linkbutton" onclick="page.searchData()">查询</a></td>
                    <td><a href="javascript:void(0)" class="easyui-linkbutton linkbutton" onclick="page.searchReset()">重置</a></td>
                    <td></td>
                </tr>
            </table>
            </form>
        </div>
    </div>
    <div class="panelSection">
        <div class="easyui-panel " title="排产结果信息" style="padding: 3px;width: 100%" data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
            <table id="container" class="easyui-treegrid"   style="height:400px"></table>
        </div>
    </div>
    <div class="panelSection">
        <div class="easyui-panel " title="按钮区域" style="padding: 3px;width: 100%" data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
            <div style="margin: 15px 0px">
                <a href="javascript:void(0)" class="easyui-linkbutton linkbutton" onclick="page.examineAndconfirmApsResult()">
                    确认审核</a>
            </div>
        </div>
    </div>
    <!--信息提示区域-->
    <div id="statusMessage" style="font-size: 14px; height: 40px">
    </div>
</body>
</html>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/ReferenceJsLib.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/CustomCommon.js" type="text/javascript"></script>
<script type="text/javascript">
    var page = {
        parentID: '', //父窗体div
        rootPath: getRootPath(), //网站根路径
        ajaxUrl: getRootPath() + "/Ashx/ApsHandler/ZSLimitedCapacityHandler.ashx",
        param: [], //需要查看的id
        comboxUrl: getRootPath() + "/Ashx/ZsHandler/ZSTestRunNoticeBillHandler.ashx", //combox下拉-调用后台的URL
        comboxAction: "GetMfgorder", //combox下拉-调用后台的具体方法
        //初始化页面
        init: function () {
            //$('#txtPlanStartTime').datebox('setValue', dayjs().startOf('month').format('YYYY-MM-DD'));
            //$('#txtPlanEndTime').datebox('setValue', dayjs().endOf('month').format('YYYY-MM-DD'));
            page.bind(null);
            page.loadInfiniteCapacity();
        },
        bind: function (dateRange) {
            var columns = [];
            var Spancolumns = [];
            var plans = $("#txtPlanStartTime").datebox('getValue');
            var plane = $("#txtPlanEndTime").datebox('getValue');
            if (dateRange != null && plans == "") {
                plans = dateRange.startdate;
                plane = dateRange.enddate;
            }
            var startDated = Date.parse(plans);
            var endDated = Date.parse(plane);
            var date = Math.abs(endDated - startDated);
            var allDay = Math.floor(date / (1000 * 3600 * 24));
            for (var i = 0; i <= allDay; i++) {
                var date = dayjs(plans).add(i, 'day'); //批次计划开始
                if (allDay <= 7) {//一周内
                    var isExist = Spancolumns.some(function (i) {
                        return i.field === date.format('MM-DD')
                    });
                    if (!isExist) {
                        Spancolumns.push({ field: date.format('MM-DD'), title: date.format('MM-DD'), colspan: 24, align: 'center' });
                    }
                    for (j = 0; j < 24; j++) {
                        var dateh = date.add(j, 'hour');
                        columns.push({ field: dateh.format('YYYY-MM-DD HH'), title: dateh.format('HH'), width: 30, align: 'center', formatter: function (value, row, index) {
                            var day = dayjs(this.field);
                            console.info((dayjs(row.PLANSTART) <= day && dayjs(row.PLANEND) >= day) + '--' + row.PLANSTART + "--" + this.field + '--' + row.PLANEND + '---' + (dayjs(dayjs(row.PLANEND).format('YYYY-MM-DD HH:mm')) >= day));
                            if (dayjs(dayjs(row.PLANSTART).format('YYYY-MM-DD HH')) <= day && dayjs(dayjs(row.PLANEND).format('YYYY-MM-DD HH')) >= day) {
                                //return '<div class="backg"  style="background-color: #409EFF;height: 15px;width:30px;"></div>';
                            }
                        }
                        });
                    }

                }

                if (allDay > 7 && allDay <= 31) {//一个月内
                    var isExist = Spancolumns.some(function (i) {
                        return i.field === date.format('YYYY-MM')
                    });
                    if (!isExist) {
                        var colspan = allDay + 1;
                        if (dayjs(plans).endOf('month').format('YYYY-MM') != dayjs(plane).endOf('month').format('YYYY-MM')) {//判断开始和结束 ---是否同一月份
                            if (Spancolumns.length == 0) {//第一个月
                                colspan = dayjs(date).endOf('month').get('date') - date.get('date') + 1;
                            } else {
                                colspan = dayjs(plane).get('date') - dayjs(date).startOf('month').get('date') + 1;
                            }
                        }
                        //console.info(colspan);
                        Spancolumns.push({ field: date.format('YYYY-MM'), title: date.format('YYYY-MM'), colspan: colspan, align: 'center' });
                    }
                    columns.push({ field: date.format('YYYY-MM-DD'), title: date.get('date'), width: 30, align: 'center', formatter: function (value, row, index) {
                        var day = dayjs(this.field); //console.info((dayjs(row.PLANEND) >= day) + '--' + row.PLANEND + "--" + this.field);
                        if (dayjs(dayjs(row.PLANSTART).format('YYYY-MM-DD')) <= day && dayjs(dayjs(row.PLANEND).format('YYYY-MM-DD')) >= day) {
                            return '<div class="backg"  style="background-color: #409EFF;height: 15px;width:30px;"></div>';
                        }
                    }
                    });
                }

                if (allDay > 31 && allDay <= 365) {//一年内
                    var smonth = dayjs(plans).get('month');
                    var emonth = dayjs(plane).get('month');
                    var isExist = Spancolumns.some(function (i) {
                        return i.field === date.format('YYYY')
                    });
                    if (!isExist) {
                        Spancolumns.push({ field: date.format('YYYY'), title: date.format('YYYY'), colspan: emonth - smonth + 1, align: 'center' });
                    }
                    var isExistM = columns.some(function (i) {
                        return i.field === date.format('YYYY-MM')
                    });
                    if (!isExistM) {
                        columns.push({ field: date.format('YYYY-MM'), title: date.format('YYYY-MM'), width: 90, align: 'center', formatter: function (value, row, index) {
                            var date = dayjs(this.field + "-01"); console.info((dayjs(row.PLANSTART).startOf('month') <= date) + '--' + (dayjs(row.PLANEND).endOf('month') >= date) + '--' + (this.field) + "--" + (row.PLANSTART) + '--' + (row.PLANEND)); if (dayjs(row.PLANSTART).startOf('month') <= date && dayjs(row.PLANEND).endOf('month') >= date) {
                                return ''; //'<div class="backg" style="background-color: #409EFF;height: 15px;width:90px;"></div>'; 
                            }
                        }
                        });
                    }
                }
            }
            $('#container').treegrid({
                collapsible: false,
                animate: true,
                //lines: true,
                checkbox: true,
                //onlyLeafCheck: false, //定义是否只在叶节点前显示复选框。
                idField: 'id',
                treeField: 'CONTAINERNAME',
                frozenColumns: [[
                 { field: 'CONTAINERNAME', title: '任务单号', width: 200, halign: 'center', align: 'left', formatter: function (value, row, index) { return (row.CN == null ? value : '<span style=\'color:red\'>[' + (row.APSTYPE.indexOf("5") != -1 ? "正排" : "倒排") + ']</span>' + value + ' <span style=\'color:blue\'>(' + row.CN + ')</span>'); } },
                 { field: 'PLANSTART', title: '计划开始', width: 150, align: 'center' },
                 { field: 'PLANEND', title: '计划完成', width: 150, align: 'center' },
                 { field: 'WORKSTATIONNAME', title: '工位', width: 100, align: 'center'}]],
                columns: [Spancolumns, columns],
                onLoadSuccess: function (row, data) {
                    page.rendRow(row, data.rows, dateRange);
                    //$('#container').treegrid('collapseAll');
                }
            });
        },
        rendRow: function (row, data, dateRange) {
            var awidth = 0;
            for (var j = 0; j < data.length; j++) {
                var rowid = $('#container').parent().find('.datagrid-view2').find('tr[node-id="' + data[j].id + '"]');
                var width = rowid.find('td').eq(0).width() + 1;
                if (width > 0) { awidth = width; } else { width = awidth; }
                var colspan = 0;
                var startDate = data[j].PLANSTART;
                var endDate = data[j].PLANEND;
                var plans = $("#txtPlanStartTime").datebox('getValue') + " 00:00:00";
                var plane = $("#txtPlanEndTime").datebox('getValue') + " 23:59:00";
                if (dateRange != null && $("#txtPlanStartTime").datebox('getValue') == "") {
                    plans = dateRange.startdate + " 00:00:00";
                    plane = dateRange.enddate + " 23:59:00";
                }
                if (dayjs(startDate) <= dayjs(plans)) { startDate = plans; } //批次开始<=选择开始
                if (dayjs(endDate) >= dayjs(plane)) { endDate = plane; } //批次结束》选择结束

                var difftype = 'hour'
                var diffday = page.getDays(plans, plane);
                if (diffday > 7 && diffday <= 31) {//日期天模式
                    difftype = 'day'
                    startDate = dayjs(startDate).format('YYYY-MM-DD');
                    endDate = dayjs(endDate).format('YYYY-MM-DD');
                }
                if (diffday > 31) {
                    difftype = 'month'
                    startDate = dayjs(startDate).format('YYYY-MM-DD');
                    endDate = dayjs(endDate).format('YYYY-MM-DD');
                }
                if (dayjs(endDate) >= dayjs(plane)) { //批次结束日期》=选择的结束日期
                    if (diffday > 7) {
                        colspan = dayjs(plane).diff(startDate, difftype) + 1
                    } else {//小时模式
                        colspan = dayjs(plane).diff(startDate, difftype, true) + 1
                    }
                } else {//批次结束日期》=选择的结束日期
                    if (diffday > 7) {
                        colspan = dayjs(endDate).diff(startDate, difftype) + 1
                        //colspan = page.getDays(startDate, endDate) + 1
                    } else {
                        colspan = dayjs(endDate).diff(startDate, difftype, true); console.info(colspan);
                        console.info(dayjs(endDate).format('YYYY-MM-DD HH') + "-------" + dayjs(startDate).format('YYYY-MM-DD HH'));
                    }
                }
                var diff_day = dayjs(startDate).diff(plans, difftype, difftype == 'hour' ? true : false).toFixed(2)
                if (dayjs(endDate) >= dayjs(startDate)) {
                    rowid.css('position', 'relative');
                    rowid.append("<div class='ganttDiv' style='width:" + width * colspan + "px;left:" + diff_day * width + "px;background:" + data[j].color + "'></div>");
                }

            }
        },

        examineAndconfirmApsResult: function () {
            var ContainerData = [];
            var nodes = $('#container').treegrid('getCheckedNodes');
            for (var i = 0; i < nodes.length; i++) {
                if (Object.hasOwn(nodes[i], 'children')) {
                    console.info(nodes[i]);
                    ContainerData.push(nodes[i]);
                }
            }
            if (ContainerData.length == 0) {
                displayMessage("请勾择需要下发的任务单",false); return;
            }
           var param = {
               action: "examineAndconfirmApsResult",
               data: JSON.stringify(ContainerData)
           }
           page.post(param, function (res) {
               displayMessage(res.Message, res.Result);
           });
        },
        getDays: function (start, end) {
            var startDated = Date.parse(start);
            var endDated = Date.parse(end);
            var date = Math.abs(endDated - startDated);
            var allDay = Math.floor(date / (1000 * 3600 * 24));
            return allDay;
        },
        searchReset: function () {
            page.reset(true, true, false);
        },
        searchData: function () {
            page.reset(false, true, false);
            var ContainerNo = $('#txtContainerNo').textbox('getValue');
            var PlanStartTime = $("#txtPlanStartTime").datebox('getValue');
            var PlanEndTime = $("#txtPlanEndTime").datebox('getValue');
            page.param = ContainerNo;
            page.loadInfiniteCapacity();
        },
        loadInfiniteCapacity: function () {
            var param = {
                action: 'getStepInfiniteAps2',
                machine: $('#txtMachine').textbox('getValue'),
                containerNo: $('#txtContainerNo').textbox('getValue'),
                mfgorderName: $('#txtMfgorderName').textbox('getValue'),
                planStartTime: $("#txtPlanStartTime").datebox('getValue'),
                planEndTime: $("#txtPlanEndTime").datebox('getValue'),
                status: 2
            }
            page.post(param, function (res) {
                page.bind(res.ReturnData.dateRange);
                $('#container').treegrid('loadData', res.ReturnData);
            });
        },
        //post数据
        post: function (param, callback) {
            $.ajax({
                type: "Post",
                url: page.ajaxUrl,
                data: param,
                cashe: false,
                async: true,
                dataType: "json",
                beforeSend: function () {
                    $.messager.progress({ text: '', msg: '处理中...' });
                },
                success: function (data) {
                    $.messager.progress('close');
                    callback(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.messager.progress('close');
                    displayMessage(XMLHttpRequest.responseText, false);
                }
            });
        },
        //清除方法，重要！！！
        reset: function (iscondition, isresult, isDetail) {
            clearMessage();
            if (iscondition) {
                $("#txtPlanStartTime").textbox("setValue", "");
                $("#txtPlanEndTime").textbox("setValue", "");
                $("#txtMfgorderName").textbox("setValue", "");
                $("#txtMachine").textbox("setValue", "");
                $("#txtContainerNo").textbox("setValue", "");
            }

            if (isresult) {
                $("#container").empty();
            }
        }
    }

$(function () {
    //获取传参
    page.parentID = getParam("parentDiv");
    page.param=[];//parent.$("#" + page.parentID).dialog('options').popupData;
    page.init();

});
</script>
<script src="/InSiteWebApplication/Scripts/ZsJs/selectCombox.js" type="text/javascript"></script>
